package com.guochuang.yunwang.promanager.serviceImpl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guochuang.yunwang.promanager.dao.ProjectMapper;
import com.guochuang.yunwang.promanager.domain.Group;
import com.guochuang.yunwang.promanager.domain.Project;
import com.guochuang.yunwang.promanager.domain.Role;
import com.guochuang.yunwang.promanager.domain.dto.ProjectDTO;
import com.guochuang.yunwang.promanager.domain.vo.ProjectVO;
import com.guochuang.yunwang.promanager.domain.vo.UserRoleVO;
import com.guochuang.yunwang.promanager.exception.ProjectAssignException;
import com.guochuang.yunwang.promanager.service.ProjectService;
import com.guochuang.yunwang.promanager.utils.ProjectConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {


    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private SimpleDateFormat simpleDateFormat;


    @Override
    public UserRoleVO getRolesList(Integer uid) {

        // 根据用户id查询已有的角色
        List<Role> userRoles = projectMapper.findUserRoles(uid);

        // 查询所有的角色
        List<Role> allRoles = projectMapper.getAllRoles();

        UserRoleVO rec = new UserRoleVO(allRoles, userRoles);

        return rec;
    }

    @Override
    @Transactional
    public Integer addNewProject(Project project) {
        String timestr = simpleDateFormat.format(System.currentTimeMillis());

        // 插入项目信息
        project.setGmtModified(timestr);

        project.setLaunchTime(project.getLaunchTime().split("T")[0]);

        project.setGmtCreate(project.getGmtCreate().split("T")[0]);

        Integer nums = projectMapper.addNewProject(project);

        if(project.getGid()!=null){
            // 添加团队
            projectMapper.insertNewRecord(project.getPid(),project.getGid(),timestr);
        }

        return nums;
    }

    @Override
    public Integer deleteByPid(Integer pid) {
        return projectMapper.deleteByPid(pid);
    }

    @Override
    public Integer addGroupToProject(Integer pid, Integer gid) {

        // 首先查询该团队是否被分配
        Integer count = projectMapper.getGroupByGid(gid);

        if(count != 0){
            throw new ProjectAssignException(ProjectConstant.PROJECT_ASSIGN_FAILED);
        }
        String timestr = simpleDateFormat.format(new Date());

        // 执行分配团队操作
        Integer nums = projectMapper.insertNewRecord(pid,gid,timestr);

        return nums;
    }

    @Override
    public PageInfo getProjectListByParams(ProjectVO projectVo) {

        PageHelper.startPage(projectVo.getPage(), projectVo.getOffset());

        // 查询数据
        List<ProjectDTO> projectdto = projectMapper.findProjectsByParams(projectVo);

        PageInfo<ProjectDTO> pageInfo = new PageInfo<>(projectdto);

        return pageInfo;
    }

    @Override
    public List<Group> getGroups() {
        return projectMapper.selectGroups();
    }

    /**
     *
     * @param page
     * @param offset
     * @param complete
     * @return
     */
    @Override
    public PageInfo getProjectListByPage(Integer page, Integer offset,
                                         String complete) {
        // 开启分页
        PageHelper.startPage(page,offset);

        // 获取数据
        List<ProjectDTO> projects = projectMapper.findProjectList(complete, 0);

        for (ProjectDTO project : projects) {
            // 对一级项目进行递归操作
            getChild(project,complete);
        }
        PageInfo pageInfo = new PageInfo(projects);

        return pageInfo;
    }

    /**
     * 获取父项目下的子项目
     * @param project
     * @return
     */
    private void getChild(ProjectDTO project,String complete){

        List<ProjectDTO> list = projectMapper.findProjectList(complete, project.getPid());

        if(list.size() == 0){
            return;
        }else{
            project.setChild(list);
            for (ProjectDTO pro : list) {
                getChild(pro,complete);
            }
        }
    }
}
